<?php
/**
 * Created by PhpStorm.
 * User: xuzhong
 * Date: 2025/4/2
 * Time: 13:49
 */

namespace app\api\service;

use app\admin\model\ManualBook as ManualBookModel;
use app\api\model\Images;
use app\facade\Basic;
use think\facade\Db;

class ManualBook
{

    protected $model;

    public function __construct()
    {
        $this->model = new ManualBookModel();

    }
    public function download($params): array
    {
        # 验证参数
        if (!isset($params['id']) || !$params['id']) {
            return Basic::data(100002, 'common.parameterError');
        }
        $where = ['is_del' => 0];
        // 获取矿池基本信息
        $pools = $this->model->where($where)
            ->where('id', $params['id'])
            ->column('file_id');
        $oImages = new Images();
        $photoUrls = $oImages->getImagesByIds($pools);
        $pools = $photoUrls[0]['url'] ?? '' ;
        return Basic::data(0, 'common.success', $pools);
    }
    /**
     * @describe 获取手册列表
     * @author: 徐众
     * @date: 2025-04-02 13:02:26
     * @param [] type
     * @return Renderable
     */
    public function getList($params): array
    {
        #验证参数
        if (!isset($params['sku_id']) || !$params['sku_id'] || !isset($params['pid']) || !$params['pid']) {
            return Basic::data( 100002, 'common.parameterError');
        }
        #处理查询条件
        $where = ['is_del' => 0];
        $sql = $this->model->where($where)->whereRaw("(FIND_IN_SET({$params['sku_id']},pids) and type = 1) or (FIND_IN_SET({$params['pid']},pids) and type = 2 )" );
        #取数据
        $list = $sql->field('id,title,type,file_id')->select()->toArray();
        $ret=[];
        $ret['certificate'] = [];
        $ret['manual'] = [];
        $oImages = new Images();
        foreach ($list as $item){
            if($item['type'] == 1){
                $ret['manual'][] = [
                    'file_id' =>$item['id'],
                    'title' =>$item['title'],
                ];
            }elseif ($item['type'] == 2){
                $pools = explode(',',$item['file_id']);
                $photoUrls = $oImages->getImagesByIds($pools);
                if(isset($ret['certificate'])){
                    $ret['certificate'] = array_merge( $ret['certificate'],$photoUrls );
                }else {
                    $ret['certificate'] = $photoUrls;
                }

            }
        }
        return Basic::data(0, 'common.success',  $ret);
    }


}